package cn.tedu.ivos.geofence.service.imp;

import cn.tedu.ivos.base.exception.ServiceException;
import cn.tedu.ivos.base.response.StatusCode;
import cn.tedu.ivos.geofence.mapper.GeofenceMapper;
import cn.tedu.ivos.geofence.pojo.dto.GeofenceParam;
import cn.tedu.ivos.geofence.pojo.dto.GeofenceQuery;
import cn.tedu.ivos.geofence.pojo.entity.Geofence;
import cn.tedu.ivos.geofence.pojo.vo.GeofenceVO;
import cn.tedu.ivos.geofence.service.GeofenceService;
import cn.tedu.ivos.vehicle.mapper.VehicleMapper;
import cn.tedu.ivos.vehicle.pojo.dto.VehicleQuery;
import cn.tedu.ivos.vehicle.pojo.vo.VehicleVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Slf4j
@Service
public class GeofenceServiceImp implements GeofenceService {
    @Autowired
    GeofenceMapper geofenceMapper;
    @Autowired
    VehicleMapper vehicleMapper;
    @Override
    public List<GeofenceVO> selectGeo(GeofenceQuery geofenceQuery) {
        log.debug("查询围栏业务开始: {}", geofenceQuery);
        List<GeofenceVO> geofenceVO = geofenceMapper.selectGeo(geofenceQuery);
        log.debug("查询围栏业务结束: {}", geofenceVO);
        for (int i = 0; i < geofenceVO.size(); i++) {
            GeofenceVO geofenceVO1 = geofenceVO.get(i);
            VehicleQuery vehicleQuery = new VehicleQuery();
            vehicleQuery.setGeofenceId(geofenceVO1.getId());
            List<VehicleVO> vehicleVOS = vehicleMapper.selectVehicle(vehicleQuery);
            int totalNum = vehicleVOS.size();
            int availableNum = 0;
            for (int j = 0; j < vehicleVOS.size(); j++) {
                if (vehicleVOS.get(j).getGeofenceBindStatus().equals("1")){
                    availableNum++;
                }

            }
            geofenceVO1.setTotalNum(totalNum);
            geofenceVO1.setAvailableNum(availableNum);
            geofenceVO1.setVehicleList(vehicleVOS);
        }

        return geofenceVO;

    }
    @Override
    public void saveGeofence(GeofenceParam geofenceParam) {
        log.debug("保存围栏信息业务:geofenceParam={}",geofenceParam);
        Geofence geofence = new Geofence();
        BeanUtils.copyProperties(geofenceParam,geofence);
        if(geofence.getId() != null){
            geofence.setUpdateTime(new Date());
            geofenceMapper.update(geofence);
        }else{
            geofence.setStatus("1");//启用状态
            geofence.setCreateTime(new Date());
            geofenceMapper.insert(geofence);
        }
    }
    @Override
    public void deleteGeofence(Long id) {
        log.debug("删除电子围栏业务:id={}",id);
        //1.封装车辆的查询条件
        VehicleQuery vehicleQuery = new VehicleQuery();
        vehicleQuery.setGeofenceId(id);
        //2.调用车辆模块的功能查询当前围栏上绑定的车辆数据
        List<VehicleVO> list = vehicleMapper.selectVehicle(vehicleQuery);
        //3.如果该围栏下有车辆,业务层需要抛出异常
        if(list.size()>0 && list != null){
            //throw new ServiceException(StatusCode.OPERATION_FAILED);
            /*抛出更加明确的异常,但注意需要在StatusCode中新增一个值*/
            throw new ServiceException(StatusCode.VEHICLE_EXISTS);
        }else{
            //4.如果该围栏下没有车辆,正常删除
            geofenceMapper.deleteById(id);
        }
    }
}
